﻿using System;

namespace yDevice.Trigger
{
    public class Timer
    {
        private DateTime startTime;
        private RisingEdge startTrigger;
        private FallingEdge recordTrigger;

        public Timer()
        {
            startTime = DateTime.Now;
            startTrigger = new RisingEdge();
            recordTrigger = new FallingEdge();
        }

        public bool CLK
        {
            set
            {
                startTrigger.Clk = value;
                recordTrigger.Clk = value;
                if (startTrigger.Q)
                {
                    startTime = DateTime.Now;
                }
                if (value == false)
                {
                    startTime = DateTime.Now;
                }
            }
        }

        public double Duration { get; set; }

        public bool Q
        {
            get
            {
                if (DateTime.Now.Subtract(startTime).TotalSeconds >= Duration)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public bool NeedRecord
        {
            get
            {
                if (recordTrigger.Q)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 复位
        /// </summary>
        public void Reset(double duration)
        {
            startTime = DateTime.Now;
            Duration = duration;
        }
    }
}
